Page({
	data: {
		nickName: 'name ',
		showModalInput: false,
		newUserName: '',
		inputShowed: true,
		avatarUrl: '/icon/无头像.png',
	},
	chooseImage: function (event) {
		let that = this;
		wx.chooseMedia({
				count: 1, 
				sizeType: ['original'], 
				sourceType: ['album'], 
				success: function (res) {
						const tempFilePaths = res.tempFilePaths;
						console.log(tempFilePaths);
						that.setData({
								avatarUrl: tempFilePaths[0]
						});
				},
				fail: function (err) {
						console.error(err);
				}
		});
},
	onLoad() {
		const app = getApp(); 
		const U_name = app.globalData.userInfo.nickName; 
		const photo = app.globalData.userInfo.avatarUrl;
		this.setData({
			nickName: U_name,
			avatarUrl: photo,
		});
	},
	showInputBox: function () {
		this.setData({
			showModalInput: true,
			newUserName: this.data.nickName
		});
	},
	confirmInput: function () {
		let newUserName = this.data.newUserName.trim();
		if (newUserName === '') {
			wx.showToast({
				title: '用户名不能为空',
				icon: 'none',
				duration: 2000
			});
			return;
		}
		this.setData({
			nickName: newUserName,
			showModalInput: false,
		});
		const app = getApp();
		app.globalData.userInfo.nickName = newUserName;

		wx.showToast({
			title: '用户名已修改',
			icon: 'success',
			duration: 2000
		});
	},

	cancelInput: function () {
		console.log('用户点击了取消输入');
		this.setData({
			showModalInput: false
		});
	},

	inputChange: function (e) {
		this.setData({
			newUserName: e.detail.value
		});
	}
})